package com.ruoyi.service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.domain.SysRole;
import com.ruoyi.domain.SysUser;
import com.ruoyi.mapper.SysRoleMapper;

@Service
public class SysRoleService {
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	public List<SysRole> selectRoleList(SysRole role) {
		return sysRoleMapper.selectRoleList(role);
	}

	public SysRole selectRoleById(Long roleId) {
		return sysRoleMapper.selectRoleById(roleId);
	}

	public boolean checkRoleNameUnique(SysRole role) {
		Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        SysRole info = sysRoleMapper.checkRoleNameUnique(role.getRoleName());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;		
	}

	public boolean checkRoleKeyUnique(SysRole role) {
		Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        SysRole info = sysRoleMapper.checkRoleKeyUnique(role.getRoleKey());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}

	public int insertRole(SysRole role) {
		return sysRoleMapper.insertRole(role);
	}

	public int updateRole(SysRole role) {
		return sysRoleMapper.updateRole(role);
	}

	public Set<String> selectRolePermissionByUserId(Long userId) {
        List<SysRole> perms = sysRoleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms) {
            if (StringUtils.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

	public int authDataScope(SysRole role) {
		return sysRoleMapper.updateRole(role);
	}

	public void checkRoleAllowed(SysRole role) {
		if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) {
            throw new ServiceException("不允许操作超级管理员角色");
        }
	}

	public void checkRoleDataScope(Long roleId) {
		if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
            SysRole role = new SysRole();
            role.setRoleId(roleId);
            List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
            if (StringUtils.isEmpty(roles)) {
                throw new ServiceException("没有权限访问角色数据！");
            }
        }
		
	}

	public int updateRoleStatus(SysRole role) {
		return sysRoleMapper.updateRole(role);
	}

	public int deleteRoleById(Long roleId) {
		return sysRoleMapper.deleteRoleById(roleId);
	}

	public List<SysRole> selectRoleAll() {
		return sysRoleMapper.selectRoleAll();
	}

	
	
}
